
import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginApi } from '@/api/user'
// 管理用户token
const state = {
  // 存储token
  token: getToken() || null
}
const mutations = {
  // 设置token
  setToken(state, payload) {
    setToken(payload)
    state.token = payload
  },
  // 清楚token
  delToken({ token }, payload) {
    removeToken()
    token = null
  }
}
const actions = {
  async toLogin({ commit }, token) {
    try {
      const { status, data: res } = await loginApi(token)
      if (status !== 200) return
      commit('setToken', res.data)
    } catch (error) {
      return Promise.reject(error)
    }
  }
}

export default {
  namespaced: 'true',
  state,
  mutations,
  actions
}

